package com.ecube.sql;

import com.ecube.mqtt.MqttUtils;
import com.ecube.mqtt.mysql.Query;
import sun.misc.PostVMInitHook;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class State {

    public static void main(String[] args) throws InterruptedException {
        int dcMeterElectAdd =34;
        int dcMeterElect = 0;

        while (true) {
            sendRealDataMQTT("FAT", "040205", dcMeterElect);
            dcMeterElect = dcMeterElectAdd + dcMeterElect;
            Thread.sleep(1000*60);
        }
    }
    public static void sendRealDataMQTT(String env, String externalCode, int dcMeterElect) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/state";
        String version = Query.getVersion(env,externalCode);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Random random = new Random();
        // 生成一个随机数，范围在0（包含）到1000（不包含）之间
        double minimum = 6000;
        double maximum = 2000;
        double randomNumber = minimum + (double) (Math.random() * maximum);
        // 格式化为两位小数
        String chgOutputVolt = String.format("%.2f", randomNumber);
        double minimum1 = 500;
        double maximum1 = 100;
        double randomNumber1 = minimum1 + (double) (Math.random() * maximum1);
        // 格式化为两位小数
        String chgOutputCurr = String.format("%.2f", randomNumber1);

        String maxPower = "800";
        String maxPower1 = "800";


        String content = " {\n" +
                "    \"header\": {\n" +
                "        \"version\": \"" + version + "\",\n" +
                "        \"timeStamp\": \"" + dateFormat.format(date) + "\",\n" +
                "        \"reason\": 2,\n" +
                "        \"index\": 135321,\n" +
                "        \"function\": \"chargeState\"\n" +
                "    },\n" +
                "    \"dataBody\": [\n" +
                "            {\n" +
                "                \"moduleNum\": 10,\n" +
                "                \"moduleList\": [\n" +
                "                    {\n" +
                "                        \"volt\": 632.9,\n" +
                "                        \"temp\": 26,\n" +
                "                        \"moduleNo\": 1,\n" +
                "                        \"curr\": 47.6\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 629.6,\n" +
                "                        \"temp\": 28,\n" +
                "                        \"moduleNo\": 2,\n" +
                "                        \"curr\": 47.8\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 629.8,\n" +
                "                        \"temp\": 27,\n" +
                "                        \"moduleNo\": 3,\n" +
                "                        \"curr\": 47.8\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 631.7,\n" +
                "                        \"temp\": 27,\n" +
                "                        \"moduleNo\": 4,\n" +
                "                        \"curr\": 47.7\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 629.8,\n" +
                "                        \"temp\": 24,\n" +
                "                        \"moduleNo\": 5,\n" +
                "                        \"curr\": 47.8\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 630.2,\n" +
                "                        \"temp\": 20,\n" +
                "                        \"moduleNo\": 6,\n" +
                "                        \"curr\": 47.8\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 629.9,\n" +
                "                        \"temp\": 17,\n" +
                "                        \"moduleNo\": 7,\n" +
                "                        \"curr\": 47.8\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 631.9,\n" +
                "                        \"temp\": 18,\n" +
                "                        \"moduleNo\": 8,\n" +
                "                        \"curr\": 47.7\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 630.6,\n" +
                "                        \"temp\": 17,\n" +
                "                        \"moduleNo\": 9,\n" +
                "                        \"curr\": 47.8\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 630.1,\n" +
                "                        \"temp\": 19,\n" +
                "                        \"moduleNo\": 10,\n" +
                "                        \"curr\": 47.8\n" +
                "                    }\n" +
                "                ],\n" +
                "                \"gunNum\": 2,\n" +
                "                \"gunList\": [\n" +
                "                    {\n" +
                "                        \"voltDemand\": 657,\n" +
                "                        \"startTime\": \"" + dateFormat.format(date) + "\",\n" +
                "                        \"soc\": 86,\n" +
                "                        \"plugTemp\": \"38,33\",\n" +
                "                        \"gunNo\": 1,\n" +
                "                        \"dcMeterElect\": 63877.2,\n" +
                "                        \"currDemand\": 489.2,\n" +
                "                        \"chgTime\": 1196,\n" +
                "                        \"chgQty\": 99.42,\n" +
                "                        \"chgOutputVolt\": " +chgOutputVolt+ ",\n" +
                "                        \"chgOutputCurr\": " +chgOutputCurr+ ",\n" +
                "                        \"chgMode\": 2,\n" +
                "                        \"batID\": \"1234133B0197\"\n" +
                "                        \"state\": \"1\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"voltDemand\": 657,\n" +
                "                        \"startTime\": \"" +dateFormat.format(date)+ "\",\n" +
                "                        \"soc\": 86,\n" +
                "                        \"plugTemp\": \"125,125\",\n" +
                "                        \"gunNo\": 2,\n" +
                "                        \"dcMeterElect\": 62803.46,\n" +
                "                        \"currDemand\": 489.2,\n" +
                "                        \"chgTime\": 1196,\n" +
                "                        \"chgQty\": 0,\n" +
                "                        \"chgOutputVolt\": " +chgOutputVolt+ ",\n" +
                "                        \"chgOutputCurr\": 2,\n" +
                "                        \"chgMode\": 2,\n" +
                "                        \"batID\": \"1234133B0197\"\n" +
                "                        \"state\": \"1\"\n" +
                "                    }\n" +
                "                ],\n" +
                "                \"chgID\": \"4004190001000002\",\n" +
                "                \"cabinetNo\": 1\n" +
                "            },\n" +
                "            {\n" +
                "                \"moduleNum\": 10,\n" +
                "                \"moduleList\": [\n" +
                "                    {\n" +
                "                        \"volt\": 662.1,\n" +
                "                        \"temp\": 22,\n" +
                "                        \"moduleNo\": 1,\n" +
                "                        \"curr\": 39.7\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 663.3,\n" +
                "                        \"temp\": 22,\n" +
                "                        \"moduleNo\": 2,\n" +
                "                        \"curr\": 39.7\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 662.3,\n" +
                "                        \"temp\": 22,\n" +
                "                        \"moduleNo\": 3,\n" +
                "                        \"curr\": 39.7\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 662.1,\n" +
                "                        \"temp\": 20,\n" +
                "                        \"moduleNo\": 4,\n" +
                "                        \"curr\": 39.7\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 661.5,\n" +
                "                        \"temp\": 18,\n" +
                "                        \"moduleNo\": 5,\n" +
                "                        \"curr\": 39.7\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 661.1,\n" +
                "                        \"temp\": 18,\n" +
                "                        \"moduleNo\": 6,\n" +
                "                        \"curr\": 39.7\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 662.3,\n" +
                "                        \"temp\": 16,\n" +
                "                        \"moduleNo\": 7,\n" +
                "                        \"curr\": 39.7\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 662.5,\n" +
                "                        \"temp\": 14,\n" +
                "                        \"moduleNo\": 8,\n" +
                "                        \"curr\": 39.7\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 663.5,\n" +
                "                        \"temp\": 14,\n" +
                "                        \"moduleNo\": 9,\n" +
                "                        \"curr\": 39.7\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"volt\": 661.8,\n" +
                "                        \"temp\": 12,\n" +
                "                        \"moduleNo\": 10,\n" +
                "                        \"curr\": 39.7\n" +
                "                    }\n" +
                "                ],\n" +
                "                \"gunNum\": 2,\n" +
                "                \"gunList\": [\n" +
                "                    {\n" +
                "                        \"voltDemand\": 700.8,\n" +
                "                        \"startTime\": \"" + dateFormat.format(date) + "\",\n" +
                "                        \"soc\": 33,\n" +
                "                        \"plugTemp\": \"18,20\",\n" +
                "                        \"gunNo\": 1,\n" +
                "                        \"dcMeterElect\": 175652.4,\n" +
                "                        \"currDemand\": 400,\n" +
                "                        \"chgTime\": 587,\n" +
                "                        \"chgQty\": 42.62,\n" +
                "                        \"chgOutputVolt\": 660.3,\n" +
                "                        \"chgOutputCurr\": 40.1,\n" +
                "                        \"chgMode\": 2,\n" +
                "                        \"batID\": \"121420379280\"\n" +
                "                        \"state\": \"1\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"voltDemand\": 700.8,\n" +
                "                        \"startTime\": \"" +dateFormat.format(date)+ "\",\n" +
                "                        \"soc\": 33,\n" +
                "                        \"plugTemp\": \"21,27\",\n" +
                "                        \"gunNo\": 2,\n" +
                "                        \"dcMeterElect\": 171438.41,\n" +
                "                        \"currDemand\": 400,\n" +
                "                        \"chgTime\": 587,\n" +
                "                        \"chgQty\": 0,\n" +
                "                        \"chgOutputVolt\": 660.3,\n" +
                "                        \"chgOutputCurr\": 0,\n" +
                "                        \"chgMode\": 2,\n" +
                "                        \"batID\": \"121420379280\"\n" +
                "                        \"state\": \"1\"\n" +
                "                    }\n" +
                "                ],\n" +
                "                \"chgID\": \"4004190001000004\",\n" +
                "                \"cabinetNo\": 2\n" +
                "            }\n" +
                "    ]\n" +
                "}";

        String content1 = " {\n" +
                "    \"header\": {\n" +
                "        \"version\": \"" + version + "\",\n" +
                "        \"timeStamp\": \"" + dateFormat.format(date) + "\",\n" +
                "        \"reason\": 2,\n" +
                "        \"index\": 135321,\n" +
                "        \"function\": \"chargerState\"\n" +
                "    },\n" +
                "    \"dataBody\": [\n" +
                "        {\n" +
                "            \"chgID\": \"4004190001000002\",\n" +
                "            \"cabinetNo\": 1,\n" +
                "            \"comm\": 1,\n" +
                "            \"emergStop\": 1,\n" +
                "            \"door\": 1,\n" +
                "            \"chgSwitch\": 1,\n" +
                "            \"batState\": 1,\n" +
                "            \"chgFault\": \"\",\n" +
                "            \"acCont\": 2,\n" +
                "            \"gunNum\": 2,\n" +
                "            \"moduleNum\": 3,\n" +
                "            \"moduleState\": \"1,2,2,2,2,2,2,2,1,1\",\n" +
                "            \"plugSameBat\": 2,\n" +
                "            \"smokeAlarm\": 1,\n" +
                "            \"batID \": \"\",\n" +
                "            \"soc\": \"100\",\n" +
                "            \"batFault\": \"\",\n" +
                "            \"chgState\": 2,\n" +
                "            \"workMode\": 1,\n" +
                "            \"batConn\": 1,\n" +
                "            \"chgRunMode\": 1,\n" +
                "            \"startType\": 1,\n" +
                "            \"maxLimitPower\": " + maxPower1 + ",\n" +
                "            \"gunList\": [\n" +
                "                {\n" +
                "                    \"gunNo\": 1,\n" +
                "                    \"chgState\": 2,\n" +
                "                    \"workMode\": 1,\n" +
                "                    \"batConn\": 1,\n" +
                "                    \"batID\": \"1001390007\",\n" +
                "                    \"soc\": 50.1,\n" +
                "                    \"batFault\": \"\",\n" +
                "                    \"currLimit\": 1,\n" +
                "                    \"limitVal\": 800,\n" +
                "                    \"connOutCont\": 2,\n" +
                "                    \"gunOutCont\": 2,\n" +
                "                    \"elecLock\": 0,\n" +
                "                    \"auxPowerVolt\": 2,\n" +
                "                    \"auxPowerState\": 2,\n" +
                "                    \"fuseAlarm\": 1,\n" +
                "                    \"stopReason\": 0,\n" +
                "                    \"dcMeterElect\": " + dcMeterElect + ",\n" +
                "                    \"plugTemp\": \"80\",\n" +
                "                    \"maxLimitPower\": 200\n" +
                "                },\n" +
                "                {\n" +
                "                    \"gunNo\": 2,\n" +
                "                    \"chgState\": 2,\n" +
                "                    \"workMode\": 1,\n" +
                "                    \"batConn\": 1,\n" +
                "                    \"batID\": \"1001390007\",\n" +
                "                    \"soc\": 50.1,\n" +
                "                    \"batFault\": \"\",\n" +
                "                    \"currLimit\": 1,\n" +
                "                    \"limitVal\": 800,\n" +
                "                    \"connOutCont\": 2,\n" +
                "                    \"gunOutCont\": 2,\n" +
                "                    \"elecLock\": 0,\n" +
                "                    \"auxPowerVolt\": 2,\n" +
                "                    \"auxPowerState\": 2,\n" +
                "                    \"fuseAlarm\": 1,\n" +
                "                    \"stopReason\": 0,\n" +
                "                    \"dcMeterElect\": " + dcMeterElect + ",\n" +
                "                    \"plugTemp\": \"100\",\n" +
                "                    \"maxLimitPower\": 200\n" +
                "                }\n" +
                "            ]\n" +
                "        },{\n" +
                "            \"chgID\": \"4004190001000004\",\n" +
                "            \"cabinetNo\": 2,\n" +
                "            \"comm\": 1,\n" +
                "            \"emergStop\": 1,\n" +
                "            \"door\": 1,\n" +
                "            \"chgSwitch\": 1,\n" +
                "            \"batState\": 1,\n" +
                "            \"chgFault\": \"\",\n" +
                "            \"acCont\": 2,\n" +
                "            \"gunNum\": 2,\n" +
                "            \"moduleNum\": 3,\n" +
                "            \"moduleState\": \"1,2,2,2,2,2,2,2,1,1\",\n" +
                "            \"plugSameBat\": 2,\n" +
                "            \"smokeAlarm\": 1,\n" +
                "            \"batID \": \"\",\n" +
                "            \"soc\": \"100\",\n" +
                "            \"batFault\": \"\",\n" +
                "            \"chgState\": 2,\n" +
                "            \"workMode\": 1,\n" +
                "            \"batConn\": 1,\n" +
                "            \"chgRunMode\": 1,\n" +
                "            \"startType\": 1,\n" +
                "            \"maxLimitPower\": " + maxPower + ",\n" +
                "            \"gunList\": [\n" +
                "                {\n" +
                "                    \"gunNo\": 1,\n" +
                "                    \"chgState\": 2,\n" +
                "                    \"workMode\": 1,\n" +
                "                    \"batConn\": 1,\n" +
                "                    \"batID\": \"1001390007\",\n" +
                "                    \"soc\": 50.1,\n" +
                "                    \"batFault\": \"\",\n" +
                "                    \"currLimit\": 1,\n" +
                "                    \"limitVal\": 800,\n" +
                "                    \"connOutCont\": 2,\n" +
                "                    \"gunOutCont\": 2,\n" +
                "                    \"elecLock\": 0,\n" +
                "                    \"auxPowerVolt\": 2,\n" +
                "                    \"auxPowerState\": 2,\n" +
                "                    \"fuseAlarm\": 1,\n" +
                "                    \"stopReason\": 0,\n" +
                "                    \"dcMeterElect\": " + dcMeterElect + ",\n" +
                "                    \"plugTemp\": \"80\",\n" +
                "                    \"maxLimitPower\": 200\n" +
                "                },\n" +
                "                {\n" +
                "                    \"gunNo\": 2,\n" +
                "                    \"chgState\": 2,\n" +
                "                    \"workMode\": 1,\n" +
                "                    \"batConn\": 1,\n" +
                "                    \"batID\": \"1001390007\",\n" +
                "                    \"soc\": 50.1,\n" +
                "                    \"batFault\": \"\",\n" +
                "                    \"currLimit\": 1,\n" +
                "                    \"limitVal\": 800,\n" +
                "                    \"connOutCont\": 2,\n" +
                "                    \"gunOutCont\": 2,\n" +
                "                    \"elecLock\": 0,\n" +
                "                    \"auxPowerVolt\": 2,\n" +
                "                    \"auxPowerState\": 2,\n" +
                "                    \"fuseAlarm\": 1,\n" +
                "                    \"stopReason\": 0,\n" +
                "                    \"dcMeterElect\": " + dcMeterElect + ",\n" +
                "                    \"plugTemp\": \"100\",\n" +
                "                    \"maxLimitPower\": 200\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        //MqttUtils.sendMQTT(env, pubTopic, content);
        MqttUtils.sendMQTT(env, pubTopic, content1);

    }
}
